/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.struts.dao;

//~--- non-JDK imports --------------------------------------------------------
import com.myapp.struts.beans.PurchaseBean;

//~--- JDK imports ------------------------------------------------------------

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author pavan
 */
public class PurchaseDao {

    private Connection con;
    private double discount;
    private Connect drv;
    private double ed;
    private double edd;
    private double friten_insurence;
    private String getItem;
    private ResultSet getItemRs;
    private String insertIntoStock;
    private String insertpurchase;
    private String itemName;
    private Vector items;
    private PreparedStatement pstmt;
    private PreparedStatement pstmt1;
    private PreparedStatement pstmt2;
    private Vector purchaseValues;
    private Vector purchase_value;
    private double qty;
    private double rate;
    private double scess;
    private double seces;
    private int status;
    private int status1;
    private int status2;
    private double tax;
    private double tx;
    private double tx1;
    private double value;
    private double value1;
    private double value2;
    private double value3;
    private double vat;
    private double vat1;
    private double vval;

    public PurchaseDao() {
        purchase_value = new Vector();
        purchaseValues = new Vector();
        items = new Vector();
    }

    public Connection getConnect() throws Exception {
        drv = new Connect();
        con = drv.mySql();

        return con;
    }

    public boolean insert(PurchaseBean pbean) {
        try {
            purchaseValues = getPurchseValue(pbean);
            pbean.setValue(purchase_value);
            System.out.println("here------>" + pbean.getItem_code().length);
            con = getConnect();
            con.setAutoCommit(false);
            for (int i = 0; i < pbean.getItem_code().length; i++) {
                System.out.println(purchaseValues.elementAt(i));
            }
            for (int i = 0; i < pbean.getItem_code().length; i++) {
                System.out.println("here------>" + i);
                insertpurchase = "insert into purchase values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                pstmt = con.prepareStatement(insertpurchase);
                pstmt.setString(1, pbean.getSname());
                itemName = getItem(pbean.getItem_code()[i]);
                items.add(itemName);
                pstmt.setString(2, itemName);
                pstmt.setString(3, pbean.getQty()[i]);
                pstmt.setString(4, pbean.getRate()[i]);
                pstmt.setString(5, pbean.getTax()[i]);
                pstmt.setString(6, pbean.getDiscount()[i]);
                pstmt.setDouble(7, (Double) purchaseValues.elementAt(i));
                pstmt.setString(8, pbean.getPid());
                pstmt.setString(9, pbean.getPdate());
                pstmt.setString(10, pbean.getEd()[i]);
                pstmt.setString(11, pbean.getLocation());
                pstmt.setString(12, pbean.getSces()[i]);
                pstmt.setString(13, pbean.getFi()[i]);
                pstmt.setString(14, pbean.getVat()[i]);
                pstmt.setString(15, null);
                pstmt.setString(16, pbean.getItem_code()[i]);

                pstmt.setDouble(17, (((Double) purchaseValues.elementAt(i)) / Double.parseDouble(pbean.getQty()[i])));
                status = pstmt.executeUpdate();
            }

            pbean.setItemName(items);
            pstmt.close();
            System.out.println("here------>End");

            if (status >= 1) {
                System.out.println("here------>Start of Stock");

                for (int i = 0; i < pbean.getItem_code().length; i++) {
                    System.out.println("here------>" + i);
                    insertIntoStock = "Insert into stock values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                    pstmt2 = con.prepareStatement(insertIntoStock);
                    pstmt2.setString(1, pbean.getLocation());
                    pstmt2.setString(2, pbean.getQty()[i]);
                    itemName = getItem(pbean.getItem_code()[i]);
                    items.add(itemName);
                    pstmt2.setString(3, itemName);
                    pstmt2.setString(4, "0");
                    pstmt2.setString(5, pbean.getQty()[i]);
                    pstmt2.setString(6, pbean.getPdate());
                    pstmt2.setDouble(7, (Double) purchaseValues.elementAt(i));
                    pstmt2.setString(8, pbean.getPid());
                    pstmt2.setString(9, null);
                    pstmt2.setString(10, pbean.getRate()[i]);
                    pstmt2.setString(11, pbean.getSname());
                    pstmt2.setString(12, "0");
                    pstmt2.setString(13, pbean.getItem_code()[i]);
                    pstmt2.setDouble(14,
                            (((Double) purchaseValues.elementAt(i)) / Double.parseDouble(pbean.getQty()[i])));
                    status2 = pstmt2.executeUpdate();
                }

                pstmt2.close();

                if (status2 >= 1) {

                    con.commit();
                    con.close();

                    return true;
                } else {
                    con.close();

                    return false;
                }
            } else {
                con.close();

                return false;
            }
        } catch (Exception ex) {
            Logger.getLogger(PurchaseDao.class.getName()).log(Level.SEVERE, null, ex);
        }

        return false;
    }

    private Vector getPurchseValue(PurchaseBean pbean) {
        purchase_value = new Vector();
        purchase_value.removeAllElements();
        for (int i = 0; i < pbean.getItem_code().length; i++) {
            qty = Double.parseDouble(pbean.getQty()[i]);
            rate = Double.parseDouble(pbean.getRate()[i]);
            tax = Double.parseDouble(pbean.getTax()[i]);
            ed = Double.parseDouble(pbean.getEd()[i]);
            discount = Double.parseDouble(pbean.getDiscount()[i]);
            seces = Double.parseDouble(pbean.getSces()[i]);
            friten_insurence = Double.parseDouble(pbean.getFi()[i]);
            vat = Double.parseDouble(pbean.getVat()[i]);

            // double value= ((qty*rate)+tax*ed/100)-(dis/100);
            double qr = qty * rate;

            tx = (qr * tax);
            tx1 = tx / 100;
            edd = (tx1 * ed) / 100;
            scess = (tx1 * seces) / 100;
            value1 = qr + (tx1 + edd + scess + friten_insurence);
            value2 = (value1 * discount) / 100;
            value3 = value1 - value2;
            vval = (value3 * vat) / 100;
            value = value3 + vval;
            purchase_value.add(value);
            System.out.println("Final Value is =" + value);
        }

        return purchase_value;
    }

    public String getItem(String itemCode) {
        try {
            getItem = "select itemname from items where item_code=?";
            pstmt1 = con.prepareStatement(getItem);
            pstmt1.setString(1, itemCode);
            getItemRs = pstmt1.executeQuery();

            if (getItemRs.next() == true) {
                itemName = getItemRs.getString(1);
            }

            System.out.println("itemName" + itemName);
            pstmt1.close();
        } catch (Exception ex) {
            Logger.getLogger(PurchaseDao.class.getName()).log(Level.SEVERE, null, ex);
        }

        return itemName;
    }
}
